﻿using LeSoft.Application.Code;
using LeSoft.Application.Bussiness;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LeSoft.Application.Web
{
    public class HandlerActionAttribute : FilterAttribute, IActionFilter
    {
        public OperationType _operationType { get; set; }

        public string _ModuleName { get; set; }
        public string _Decription { get; set; }

        public HandlerActionAttribute(OperationType Mode, string ModuleName="",string description="")
        {
            _operationType = Mode;
            _ModuleName = ModuleName;
            _Decription = description;
        }
        public  void OnActionExecuted(ActionExecutedContext filterContext)
         {     
             //执行Action后执行
         }

        public  void OnActionExecuting(ActionExecutingContext filterContext)
        {
               //Action执行后执行
                LogEntity logEntity = new LogEntity();
                logEntity.CategoryId = 3;
                logEntity.OperateTypeId = ((int)_operationType).ToString();
                logEntity.OperateType = EnumAttribute.GetDescription(_operationType);
                logEntity.OperateAccount = OperatorProvider.Provider.Current().Account;
                logEntity.OperateUserId = OperatorProvider.Provider.Current().UserId;
                logEntity.Host = filterContext.HttpContext.Server.MachineName;
                logEntity.Browser = filterContext.HttpContext.Request.Browser.Browser;
                logEntity.ModuleId = SystemInfo.CurrentModuleId;
                logEntity.Module = _ModuleName;
                logEntity.ExecuteResult = filterContext.HttpContext.Response.StatusCode==200?1:0;
                logEntity.ExecuteResultJson = "访问地址：" +  filterContext.HttpContext.Request.FilePath;
                logEntity.Description = _Decription;
                logEntity.WriteLog();
        }
    }
}